<template>
  <div class="white-box">
    <div class="table-mb">
      <el-button type="primary" @click="handleAdd({subway_id:subway_id})" v-if="subway_id">添加站点</el-button>
    </div>

    <div class="task-one">
      <div class="task-one__item">
        <span class="task-one__label">站点名称：</span>
        <el-input placeholder="请输入站点名称" v-model="searchParams.subway_station_title" @keyup.enter.native="handleSearch"
                  clearable></el-input>
      </div>
      <div class="task-one__item">
        <span class="task-one__label">是否停用：</span>
        <el-select v-model="searchParams.is_stop" placeholder="请选择是否停用" clearable>
          <el-option label="是" :value="1"/>
          <el-option label="否" :value="2"/>
        </el-select>
      </div>
      <div class="task-one__btn clearfix">
        <el-button type="primary" :loading="searchBtnLoading" :icon="Search" @click="handleSearch">搜索</el-button>
      </div>
    </div>

    <el-table class="table-mb" v-loading="loading" :data="list" :header-cell-style="{ background: '#F5F7FA' }"
              element-loading-background="rgba(255, 255, 255, 0.6)">
      <el-table-column prop="subway_no" label="所属地铁">
        <template #default="{ row }">
          {{row.province}}-{{row.city}}-{{row.subway_no}}号线
        </template>
      </el-table-column>
      <el-table-column prop="subway_station_title" label="地铁站名称"></el-table-column>
      <el-table-column prop="lng" label="经度,纬度">
        <template #default="{ row }">
          <div>{{row.lng}},{{row.lat}}</div>
        </template>
      </el-table-column>
      <el-table-column prop="sort" label="站点序号"></el-table-column>
      <el-table-column prop="is_stop" label="是否停用">
        <template #default="{ row }">
          <el-text v-if="row.is_stop==1" type="info">停用</el-text>
          <el-text v-if="row.is_stop==2" type="success">正常</el-text>
        </template>
      </el-table-column>
      <el-table-column prop="created_at" label="创建时间"></el-table-column>
      <el-table-column label="操作" width="200" fixed="right">
        <template #default="{ row }">
          <el-button link type="primary" @click="handleEdit(row,{subway_station_id:row.subway_station_id,subway_id:row.subway_id})">编辑</el-button>
          <el-popconfirm :persistent="false" title="确定删除吗？" confirm-button-text="确定" cancel-button-text="取消"
                         @confirm="handleDelete(row)">
            <template #reference>
              <el-button link type="danger">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination background v-model:current-page="page" v-model:page-size="pageSize" :page-sizes="pageSizeNum"
                   :total="totalNums" layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>


    <el-dialog v-model="dialogShow" :title="dialogParams.type == 'add'?'添加':'编辑'" width="660px" :append-to-body="true" :destroy-on-close="true"
               :close-on-click-modal="false" @close="closeDialog">
      <el-form ref="dialogFormRef" :model="dialogForm" :rules="dialogFormRules" label-width="120px">

        <el-form-item label="站点名称" prop="subway_station_title" required>
          <el-input v-model.trim="dialogForm.subway_station_title" clearable placeholder="请输入站点名称"/>
        </el-form-item>
        <el-row :gutter="10">
          <el-col :span="10">
            <el-form-item label="经度" prop="lat" required>
              <el-input v-model.trim="dialogForm.lat" clearable placeholder="请输入纬度" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="经度" prop="lng" required  label-width="50px">
              <el-input v-model.trim="dialogForm.lng" clearable placeholder="请输入经度" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-button type="primary" @click="handleLon">获取经纬度</el-button>
          </el-col>
        </el-row>
        <el-form-item label="站点序号" prop="sort" required>
          <el-input v-model.trim="dialogForm.sort" clearable placeholder="请输入站点序号" oninput="value=value.replace(/[^\d]/g,'')" style="width: 30%"/>
          <el-text type="danger" style="margin-left: 5px">格式：数字，小的在前显示</el-text>
        </el-form-item>
        <el-form-item>
          <div class="flexs">
            <el-button type="primary" :loading="dialogParams.loading" @click="submitDialogForm(dialogFormRef)">
              确认
            </el-button>
            <el-button @click="resetDialogForm(dialogFormRef)">重置</el-button>
          </div>
        </el-form-item>
      </el-form>
    </el-dialog>

    <go-back/>
  </div>
</template>

<script setup>
import {
  getCurrentInstance,
  ref,
  reactive,
  onMounted,
  watch
} from "vue"
import listMixins from "@mixins/list"
import {
  useRoute,
  useRouter
} from "vue-router"
import {
  Search
} from "@element-plus/icons-vue"
import GoBack from "@components/GoBack.vue"

const {
  proxy
} = getCurrentInstance()

/**
 * minxin列表加载
 */
const {
  page,
  pageSizeNum,
  pageSize,
  loading,
  totalNums,
  searchParams,
  searchBtnLoading,
  apiPath,
  list,
  dialogFormRef,
  dialogShow,
  dialogForm,
  dialogParams,
  handleSearch,
  getData,
  handleAdd,
  handleEdit,
  resetDialogForm,
  handleAllSelected,
  submitDialogForm,
  closeDialog,
  handleAction
} = listMixins()

const router = useRouter()
const route = useRoute()
const subway_id = route.query.subway_id || ''
searchParams.subway_id = subway_id

dialogForm.subway_station_title = '';
dialogForm.lat = '';
dialogForm.lng = '';
dialogForm.sort = '';
dialogForm.is_stop = 2;

apiPath.value = 'admin/subway/station/index';
dialogParams.addApiPath = 'admin/subway/station/create';
dialogParams.editApiPath = 'admin/subway/station/update';

onMounted(() => {
  getData()
})

const dialogFormRules = reactive({
  subway_station_title: [{
    required: true,
    message: "请输入站点名称",
    trigger: "blur"
  }],
  lat: [{
    required: true,
    message: "请输入纬度",
    trigger: "blur"
  }],
  lng: [{
    required: true,
    message: "请输入经度",
    trigger: "blur"
  }],
  sort: [{
    required: true,
    message: "请输入站点序号",
    trigger: "blur"
  }]
})


const handleDelete=(row)=> {
  handleAction('admin/subway/station/delete', {
    subway_station_id: row.subway_station_id,
  })
}
const handleLon = ()=> {
  window.open("https://lbs.qq.com/getPoint/");
}
</script>

<style lang="less">

</style>